﻿/****************************************************
	文件：PlayerController.cs
	作者：林文豪
	日期：2021/03/21 18:37:27
	功能：1、角色控制，WASD控制移动，空格跳跃，左Shift加速
*****************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MyGame.Character {
    public class PlayerController : ICharacter {
        public float Speed, Force;

        public Transform CheckGround;
        public float     radius;
        public LayerMask GroundLayerMask;

        private Animator    _AniPlayer;
        private Rigidbody2D _Rig2D;
        private bool        _IsOnGround; // 角色是否在地上

        void Start() {
            _AniPlayer = this.GetComponent<Animator>();
            _Rig2D     = this.GetComponent<Rigidbody2D>();
        }

        void Update() {
            _IsOnGround = Physics2D.OverlapCircle(CheckGround.position, radius, GroundLayerMask) != null;
            if (Input.GetKeyDown(KeyCode.Space)) {
                _Rig2D.AddForce(new Vector2(0, Force));
            }
            AnimatorCtrl();
            PlayerFlip();
        }

        private void FixedUpdate() {
            _Rig2D.velocity = new Vector2(Input.GetAxis("Horizontal") * Speed, _Rig2D.velocity.y);
        }

        private void OnDrawGizmos() {
            Gizmos.color = Color.red;
            Gizmos.DrawSphere(CheckGround.position, radius);
        }

        // 移动时角色自动旋转
        private void PlayerFlip() {
            if (Input.GetAxisRaw("Horizontal") < 0) {
                this.transform.eulerAngles = new Vector3(0, 180, 0);
            }
            else {
                this.transform.eulerAngles = new Vector3(0, 0, 0);
            }
        }

        // 动画控制器，用于输入指定按键播放指定动画
        private void AnimatorCtrl() {
            if (Input.GetKeyDown(KeyCode.Space)) {
                _AniPlayer.SetTrigger("Jump");
            }
            else if (Input.GetKeyDown(KeyCode.J)) {
                _AniPlayer.SetTrigger("Attack_01");
            }
            else if (Input.GetKeyDown(KeyCode.K)) {
                _AniPlayer.SetTrigger("Attack_02");
            }
            else if (Input.GetKeyDown(KeyCode.F1)) {
                _AniPlayer.SetTrigger("Death");
            }
            if (Input.GetAxisRaw("Horizontal") != 0) {
                if (_IsOnGround == false) {
                    _AniPlayer.SetBool("Run", false);
                    _AniPlayer.SetBool("Idle", false);
                    return;
                }
                _AniPlayer.SetBool("Run", true);
                _AniPlayer.SetBool("Idle", false);
            }
            if (Input.GetAxisRaw("Horizontal") == 0) {
                if (_IsOnGround == false) {
                    _AniPlayer.SetBool("Run", false);
                    _AniPlayer.SetBool("Idle", false);
                    return;
                }
                _AniPlayer.SetBool("Idle", true);
                _AniPlayer.SetBool("Run", false);
            }
        }
    }

}
